package com.service.impl;

import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.entity.*;
import com.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dao.StudentDao;
import com.service.StudentService;

@Service("studentService")
@EnableTransactionManagement
public class StudentServiceImpl implements StudentService, AccountService {
	@Autowired
	private StudentDao studentDao;

	public StudentDao getStudentDao() {
		return studentDao;
	}

	public void setStudentDao(StudentDao studentDao) {
		this.studentDao = studentDao;
	}

	public List<Student> getAllStudent() throws SQLException, ClassNotFoundException {
		return studentDao.getAllStudent();
	}
	public List<Student> getStudentByName(String name) throws SQLException, ClassNotFoundException{
		return studentDao.getStudentByName(name);
	}
	@Transactional(value="transactionManager",propagation = Propagation.REQUIRED,rollbackFor={ArithmeticException.class})
	public boolean addStudent(Student student) throws IllegalArgumentException, IllegalAccessException, SQLException,ArithmeticException {
		boolean i  = studentDao.addStudent(student) >0?true:false;
		//int j = 1/0;
		Student student2 = new Student(String.valueOf(Integer.parseInt(student.getSno())+1),student.getName());
		boolean t  = studentDao.addStudent(student2) >0?true:false;
		return false;
	}
	@Transactional(value="transactionManager",propagation = Propagation.REQUIRED,rollbackFor={ArithmeticException.class})
	public boolean updateStudent(Student student) {
		int j = 1/0;
		return false;
	}
	public Student loginStudentByName(String name){
		return studentDao.loginStudentByName(name);
	}

	/***
	 * 通过用户名获取权限资源
	 *
	 * @param username
	 * @return
	 */
	public List<String> getPermissionsByStudentName(String username) {
		System.out.println("调用");
		Account user = studentDao.loginStudentByName(username);
		if(user==null){
			return null;
		}
		List<String> list = new ArrayList<String>();
		for(AccountRole userRole:user.getAccountRoles()){
			Role role = userRole.getRole();
			List<Permission> permissions = role.getPermissions();
			for (Permission p : permissions) {
				list.add(p.getUrl());
			}
		}
		return list;
	}
	public List<Map> select(String methodname){
		return studentDao.select(methodname);
	}

	@Override
	public Account getAccountByAccountName(String name) {
		// TODO Auto-generated method stub
		System.out.println(name+" 123455");
		return loginStudentByName(name);
	}

}
